package com.acme.bankapp;

import com.acme.bankapp.service.networking.worker.CommandWorker;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.service.bank.BankSaver;
import com.acme.bankapp.service.networking.BankServer;
import com.acme.bankapp.service.networking.ServerWorker;

import java.io.File;
import java.io.IOException;

/**
 * @author Gregory Sobko
 */
public class BankServerApp {
    public static int PORT = 4321;
    public static String storage = "src/main/resources/bank.data";

    public static void main(String... args) throws ClassNotFoundException, IOException {
        File file = new File(storage);
        Bank bank = null;
        if(!file.exists()){
            file.createNewFile();
            bank = new Bank();
        }else{
             bank = BankSaver.readBank(storage);
        }
        try{
            ServerWorker worker = new CommandWorker(bank);
            BankServer server = new BankServer(PORT);
            server.runServer(worker);
        }finally {
            BankSaver.saveBank(storage, bank);
        }
    }

}
